<?php
namespace app\api\controller;
use \think\Controller;
use think\Db;
// use think\Controller;
use think\Session;
use think\Request;
use think\File;
use think\Cache;
// use think\File;
class HomeController extends Controller
{
	/**
	 * 晒家多图上传
	 */
	public function imagsadd()
	{
		$list = input('param.');
        $files = request()->file('image');
	    foreach($files as $file){
	        // 移动到框架应用根目录/public/uploads/ 目录下
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
	        // dump($info);
	        // cache('image', $info, 3600);
	        if($info){

	            // 成功上传后 获取上传信息
	            $imagesName =  $info->getSaveName();
	            $img[] = 'http://www.mochoushop.com/uploads/'.$imagesName;
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }    
	    }
	    $list['home_images'] = implode(",",$img);
	    $list['time'] = date('Y-m-d');
	    if (db('home')->insert($list) > 0) {
	    	echo json_encode(array("status"=>1,'message'=>'发布成功!',"data"=>[]));
            exit;
	    }else{
	    	echo json_encode(array("status"=>2,'message'=>'发布失败!',"data"=>[]));
            exit;
	    }
	}


	
	/**
     * 首页晒家展示
     */
    public function indeximages()
    {
    	// $homeImages = db('home')->order('time desc')->limit(10)->select();
        $uid = input('param.uid');
        $homeImages = Db::name('home')
        ->alias('a')
        ->join('drm_account b','b.uid = a.uid')
        ->order('hid desc')
        ->limit(10)
        ->select();
        
    	$indeximages = [];
    	foreach ($homeImages as $key => $value) {
    		// unset($value['home_images']);
            unset($value['aid']);
            unset($value['date']);
            unset($value['vocation']);
            unset($value['age']);
            unset($value['balance']);
            unset($value['sex']);
    		$indeximages[$key] = $value;
    		$indeximages[$key]['homeImages'] = explode(",",$value['home_images']);
    		unset($indeximages[$key]['home_images']);

    	}
        $listTow = [];
        foreach ($indeximages as $key => $value) {
            $listTow[$key] = $value;
            // $userList = db('member')->where(['uid'=>$uid])->select();
            $homeClick = db('home_click')->where(['hid'=>$value['hid'],'uid'=>$uid])->select();
            if (!empty($homeClick)) {
                foreach ($homeClick as $kk => $vv) {
                    if ($value['hid'] == $vv['hid']) {
                        $listTow[$key]['user_click'] = '1';
                    }
                }
            }else{
                $listTow[$key]['user_click'] = '0';
            }
                
           
        }
        // dump($indeximages);
        // exit;
    	if (!empty($homeImages)) {
    		echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$listTow));
            exit;
    	}else{
    		echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
    	}
            
    }



    /**
     * 晒家页面列表
     */
    public function homelist()
    {

        $page = input('param.page');
        $uid = input('param.uid');
        // echo $page;
        // exit;
        $size = input('param.size');
    	$count = db('home')->order('hid desc')->count();
        if ($count == '0') {
            $json['list'] = [];
            echo json_encode(array("status"=>1,'message'=>'暂无订单数据!',"data"=>$json));
            exit;
        }
        if (empty($size)) {
            $list_max_rows = '10';
        } else {
            $list_max_rows = $size;
        }
        $total_page = ceil($count/$list_max_rows);
        $current_page = (int)$page;

        if ($count > 0) {
            $list = db('home')->order('hid desc')->page($page,$list_max_rows)->select();
        }
        $homeImages = [];
        foreach ($list as $key => $value) {
        	$homeImages[$key] = $value;
            $userList = db('account')->where(['uid'=>$value['uid']])->select();
            foreach ($userList as $kk => $vv) {
                if ($value['uid'] == $vv['uid']) {
                    $homeImages[$key]['phone'] = $vv['phone'];
                    $homeImages[$key]['user_img'] = $vv['user_img'];
                    $homeImages[$key]['user_name'] = $vv['user_name'];
                }
            }
    		$homeImages[$key]['homeImages'] = explode(",",$value['home_images']);
    		unset($homeImages[$key]['home_images']);
        }
        $listTow = [];
        foreach ($homeImages as $key => $value) {
            $listTow[$key] = $value;
            // $userList = db('member')->where(['uid'=>$uid])->select();
            $homeClick = db('home_click')->where(['hid'=>$value['hid'],'uid'=>$uid])->select();
            if (!empty($homeClick)) {
                foreach ($homeClick as $kk => $vv) {
                    if ($value['hid'] == $vv['hid']) {
                        $listTow[$key]['user_click'] = '1';
                    }
                }
            }else{
                $listTow[$key]['user_click'] = '0';
            }
                
           
        }
        // dump($homeImages);
        // exit;
        $result = [
            'count' => $count,
            'list_max_rows' => $list_max_rows,
            'list_rows' => count($list),
            'total_page' => $total_page,
            'current_page' => $current_page,
            'list' => $listTow
        ];
        echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$result));
        exit;
    }







    /**
     * 晒家用户评论
     */
    public function comment()
    {
        $list = input('param.');
        $list['time'] = date('Y-m-d');
        if (db('comment')->insert($list) > 0) {
            db('home')->where(['hid'=>$list['hid']])->setInc('comment_count');
            echo json_encode(array("status"=>1,'message'=>'评论成功!',"data"=>[]));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'评论失败!',"data"=>[]));
            exit;
        }
    }



    /**
     * 晒家用户点赞
     */
    public function home_click()
    {
        $hid = input('param.hid'); //晒家ID
        $uid = input('param.uid'); //用户id
        $homeClickList = db('home_click')->where(['hid'=>$hid,'uid'=>$uid])->find();
        if (empty($homeClickList)) {
            $list['uid'] = $uid;
            $list['hid'] = $hid;
            $list['count'] = '1';
            $list['time'] = date('Y-m-d H:i');
            if (db('home_click')->insert($list) > 0) {
                db('home')->where(['hid'=>$hid])->setInc('click');
                echo json_encode(array("status"=>1,'message'=>'点赞成功!',"data"=>[]));
                exit;
            }else{
                echo json_encode(array("status"=>2,'message'=>'点赞失败!',"data"=>[]));
                exit;
            }
        }else{
            
            if (db('home_click')->where(['hid'=>$hid,'uid'=>$uid])->delete() > 0) {
                db('home')->where(['hid'=>$hid])->setDec('click');
                echo json_encode(array("status"=>1,'message'=>'取消点赞成功!',"data"=>[]));
                exit;
            }else{
                echo json_encode(array("status"=>2,'message'=>'取消点赞失败!',"data"=>[]));
                exit;
            }
        }
    }


    /**
     * 用户评论点赞
     */
    public function comment_click()
    {
        $comment_id = input('param.comment_id'); //评论ID
        $uid = input('param.uid'); //用户id
        $commentClickList = db('comment_click')->where(['comment_id'=>$comment_id,'uid'=>$uid])->find();
        if (empty($commentClickList)) {
            $list['uid'] = $uid;
            $list['comment_id'] = $comment_id;
            $list['count'] = '1';
            $list['time'] = date('Y-m-d H:i');
            if (db('comment_click')->insert($list) > 0) {
                db('home')->where(['hid'=>$hid])->setInc('comment_count');
                db('comment')->where(['cid'=>$comment_id])->setInc('click');
                echo json_encode(array("status"=>1,'message'=>'点赞成功!',"data"=>[]));
                exit;
            }else{
                echo json_encode(array("status"=>2,'message'=>'点赞失败!',"data"=>[]));
                exit;
            }
        }else{
            
            if (db('comment_click')->where(['comment_id'=>$comment_id,'uid'=>$uid])->delete() > 0) {
                db('home')->where(['hid'=>$hid])->setDec('comment_count');
                db('comment')->where(['cid'=>$comment_id])->setDec('click');
                echo json_encode(array("status"=>1,'message'=>'取消点赞成功!',"data"=>[]));
                exit;
            }else{
                echo json_encode(array("status"=>2,'message'=>'取消点赞失败!',"data"=>[]));
                exit;
            }
        }
    }




    /**
     * 晒家详情
     */
    public function home_index()
    {
        $hid = input('param.hid');
        $uid = input('param.uid');
        //查询晒家信息
        $homeList = db('home')->where(['hid'=>$hid])->find();
        $clickList = db('home_click')->where(['uid'=>$uid,'hid'=>$hid])->find();
        $userList = db('account')->where(['uid'=>$homeList['uid']])->find();
        if (empty($clickList)) {
            $homeList['phone'] = $userList['phone'];
            $homeList['user_name'] = $userList['user_name'];
            $homeList['user_img'] = $userList['user_img'];
            $homeList['user_click'] = '0';
        }else{
            $homeList['phone'] = $userList['phone'];
            $homeList['user_name'] = $userList['user_name'];
            $homeList['user_img'] = $userList['user_img'];
            $homeList['user_click'] = '1';
        }
        $list[] = $homeList;
        $homeListTow = [];
        foreach ($list as $key => $value) {
            $homeListTow[$key] = $value;
            $homeListTow[$key]['homeImages'] = explode(",",$value['home_images']);
            unset($homeListTow[$key]['home_images']);
        }
        // exit;
        if (!empty($homeList)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$homeListTow));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }   



    /**
     * 评论列表
     */
    public function comment_list()
    {
        $hid = input('param.hid');
        $uid = input('param.uid');
        //查询晒家评论信息
        $commentList = db('comment')->where(['hid'=>$hid])->order('cid desc')->select();
        $commentClickList = [];
        foreach ($commentList as $key => $value) {
            $commentClickList[$key] = $value;
            $userClick = db('comment_click')->where(['uid'=>$uid,'comment_id'=>$value['cid']])->select();
            $userList = db('account')->where(['uid'=>$value['uid']])->select();
            foreach ($userList as $kk => $vv) {
                // dump($vv);
                if ($value['uid'] == $vv['uid']) {
                    $commentClickList[$key]['phone'] = $vv['phone'];
                    $commentClickList[$key]['user_name'] = $vv['user_name'];
                    $commentClickList[$key]['user_img'] = $vv['user_img'];
                }
            }
            if (empty($userClick)) {
                $commentClickList[$key]['user_click'] = '0';
            }
            foreach ($userClick as $k => $v) {
                if ($value['cid'] == $v['comment_id']) {
                    $commentClickList[$key]['user_click'] = '1';
                }else{
                    $commentClickList[$key]['user_click'] = '0';
                }
            }
        }
        // exit;
        if (!empty($commentList)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$commentClickList));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }



}

    